cs4hs11.rsalibrary
Class RSAMath

java.lang.Object
  extended by cs4hs11.rsalibrary.RSAMath

public class RSAMath
extends java.lang.Object


Constructor Summary
RSAMath()
          Empty constructor.
 
Method Summary
 int coprime(int x)
          Using a java.util.Random number generator, pick a random integer that is coprime to the given input parameter x.
 int endecrypt(int msg_or_cipher, int key, int c)
          Given an integer representing an ASCII character value, encrypt it via the RSA crypto algorithm.
 int GCD(int a, int b)
          Computes the GCD of two numbers a and b
 int mod_inverse(int base, int m)
          Compute the modular inverse base^-1 % m.
 int modulo(int a, int b, int c)
          Computes Math.mod(Math.pow(a, b), c), for large values of a, b, and c
 int totient(int n)
          Compute Euler's Totient.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RSAMath

public RSAMath()
Empty constructor. This class contains no state; as a result, this class could contain no more than static utility functions. It is instead implemented as a standard class with no state, and a suite of self-contained functions.

Method Detail

GCD

public int GCD(int a,
               int b)
Computes the GCD of two numbers a and b

Parameters:
a - One of the two numbers to compute the GCD on
b - One of the two numbers to compute the GCD on
Returns:
The GCD of the input parameters a and b, as an integer

modulo

public int modulo(int a,
                  int b,
                  int c)
Computes Math.mod(Math.pow(a, b), c), for large values of a, b, and c

Parameters:
a - The base value a in a^b % c
b - The exponent value b in a^b % c
c - The modulus value c in a^b % c
Returns:
The mod power a^b mod c

totient

public int totient(int n)
Compute Euler's Totient. From Wikipedia: The totient of a positive integer n is defined to be the number of positive integers less than or equal to n that are coprime to n (in other words, they have no common positive factors other than 1). In terms of the GCD, the Totient is the number of numbers for each i such that 1 <= i <= n, with gcd(i, n) = 1. In particular, tot(1) = 1, since 1 is coprime to itself (1 is the only natural number with this property). tot(9) = 6, since the nubmers 1, 2, 4, 5, 7, and 8 are coprime to 9. This is important in the RSA cryptosystem, because Euler's theorem goes on to state that a^(tot(n)) = 1 (mod n) for all a coprime to n.

Parameters:
n - The number to compute the Totient on
Returns:
The Euler Totient value tot(n)

coprime

public int coprime(int x)
Using a java.util.Random number generator, pick a random integer that is coprime to the given input parameter x.

Parameters:
x - An integer for which a coprime number is desired.
Returns:
Any coprime integer to the input x.

mod_inverse

public int mod_inverse(int base,
                       int m)
Compute the modular inverse base^-1 % m. Note this is not the arithmetic inverse (1/base) mod m.

Parameters:
base - The base value for which the modular inverse mod m is desired.
m - The modulo parameter m for which the modular inverse of base mod m is desired.
Returns:
The modulo inverse of base mod m; in other words, (base^-1) mod m.

endecrypt

public int endecrypt(int msg_or_cipher,
                     int key,
                     int c)
Given an integer representing an ASCII character value, encrypt it via the RSA crypto algorithm. Or, given an integer representing an encrypted ASCII value, decrypt it via the RSA crypto algorithm. Encryption is performed by taking the public key (which could have been either RSA key computed during key creation) modulo parameter C, and the ASCII value X (in msg_or_cipher), and computing (X^key) mod c. Decryption is done in the same way, but Y (msg_or_cipher) is passed instead as the encrypted integer value, and the key passed is the private key. This process will work regardless of which key is used, so the private key can be used to encrypt values that the public key will decrypt (since they are modular inverses of one another), thanks to Euler's Theorem. This is useful for computing message signatures.

Parameters:
msg_or_cipher - An integer containing the ASCII value to be encrypted or the encrypted integer value to be decrypted back to an ASCII value.
key - The public key or private key value as an integer, as appropriate.
c - The modulo parameter for performing encryption or decryption. This value is part of the public and private key (it is shared between the two!).
Returns: